Page({
  data: {
    // 页面状态
    updateTime: '',
    
    // 积分数据
    pointsData: {
      totalPoints: 1258,        // 总积分
      todayPoints: 50,          // 今日获得积分
      yesterdayPoints: 80,      // 昨日获得积分
    },
    
    // 积分统计
    pointsStats: {
      thisMonth: 320,           // 本月累计积分
      lastMonth: 280,           // 上月累计积分
    },
    
    // 积分规则
    pointsRules: [
      { id: 1, name: '购物消费', desc: '每消费1000元获得1积分'},
      { id: 2, name: '评价订单', desc: '评价订单获得5积分'},
      { id: 3, name: '会员日', desc: '会员日消费双倍积分' },
      { id: 4, name: '生日特权', desc: '生日当月消费三倍积分' }
    ],
    
    // 积分记录
    pointsRecords: [
      { id: 1, date: '2025-06-12', type: '购物消费', desc: '购买新品', points: 20 },
      { id: 2, date: '2025-06-10', type: '评价订单', desc: '评价订单', points: 5 },
      { id: 3, date: '2025-06-05', type: '会员日活动', desc: '会员日消费获得额外积分', points: 100 },
      { id: 4, date: '2025-06-01', type: '生日特权', desc: '生日当月消费双倍积分', points: 50 }
    ],
    
    // 积分兑换商品
    exchangeItems: [
      { id: 1, name: '100元无门槛优惠券', points: 100 },
      { id: 2, name: '200元满减券', points: 200 },
      { id: 3, name: '500元会员专享券', points: 500 }
    ],
    
    // 赚取积分任务
    earnTasks: [
      { id: 1, name: '完善个人信息', points: 50, completed: false },
      { id: 2, name: '绑定手机号码', points: 100,  completed: true },
      { id: 3, name: '评价订单', points: 10, completed: false },
      { id: 4, name: '参与问卷调查', points: 30,  completed: false }
    ],
    
    // 弹窗状态
    showExchangeModal: false,
    showEarnModal: false,
    hasMoreRecords: true
  },

  onLoad() {
    // 更新时间
    const now = new Date();
    this.setData({
      updateTime: now.toLocaleDateString() + ' ' + now.toLocaleTimeString()
    });
  },

  // 返回上一页
  goBack() {
    wx.navigateBack();
  },

  // 打开积分兑换弹窗
  openExchangeModal() {
    this.setData({
      showExchangeModal: true
    });
  },

  // 关闭积分兑换弹窗
  closeExchangeModal() {
    this.setData({
      showExchangeModal: false
    });
  },

  // 打开赚取积分弹窗
  showEarnPointsModal() {
    this.setData({
      showEarnModal: true
    });
  },

  // 关闭赚取积分弹窗
  closeEarnModal() {
    this.setData({
      showEarnModal: false
    });
  },

  // 兑换积分
  exchangePoints(e) {
    const itemId = e.currentTarget.dataset.id;
    const item = this.data.exchangeItems.find(item => item.id === itemId);
    
    if (!item) return;
    
    // 检查积分是否足够
    if (this.data.pointsData.totalPoints < item.points) {
      wx.showToast({
        title: '积分不足',
        icon: 'none'
      });
      return;
    }
    
    wx.showModal({
      title: '确认兑换',
      content: `确定要使用 ${item.points} 积分兑换 ${item.name} 吗？`,
      success: (res) => {
        if (res.confirm) {
          // 更新积分数据
          const newTotalPoints = this.data.pointsData.totalPoints - item.points;
          const newTodayPoints = this.data.pointsData.todayPoints;
          
          this.setData({
            'pointsData.totalPoints': newTotalPoints,
            'pointsData.todayPoints': newTodayPoints
          });
          
          // 添加兑换记录
          const newRecord = {
            id: this.data.pointsRecords.length + 1,
            date: new Date().toISOString().slice(0, 16).replace('T', ' '),
            type: '积分兑换',
            desc: `兑换${item.name}`,
            points: -item.points
          };
          
          this.setData({
            pointsRecords: [newRecord, ...this.data.pointsRecords]
          });
          
          // 关闭弹窗
          this.closeExchangeModal();
          
          // 显示成功提示
          wx.showToast({
            title: '兑换成功',
            icon: 'success'
          });
        }
      }
    });
  },

  // 完成赚取积分任务
  completeTask(e) {
    const taskId = e.currentTarget.dataset.id;
    const taskIndex = this.data.earnTasks.findIndex(task => task.id === taskId);
    
    if (taskIndex === -1) return;
    
    // 检查任务是否已完成
    if (this.data.earnTasks[taskIndex].completed) {
      wx.showToast({
        title: '任务已完成',
        icon: 'none'
      });
      return;
    }
    
    // 模拟完成任务
    wx.showLoading({
      title: '任务处理中'
    });
    
    setTimeout(() => {
      wx.hideLoading();
      
      // 更新任务状态
      const newEarnTasks = [...this.data.earnTasks];
      newEarnTasks[taskIndex].completed = true;
      
      // 更新积分数据
      const taskPoints = newEarnTasks[taskIndex].points;
      const newTotalPoints = this.data.pointsData.totalPoints + taskPoints;
      const newTodayPoints = this.data.pointsData.todayPoints + taskPoints;
      
      // 添加积分记录
      const newRecord = {
        id: this.data.pointsRecords.length + 1,
        date: new Date().toISOString().slice(0, 16).replace('T', ' '),
        type: '赚取积分',
        desc: `完成任务"${newEarnTasks[taskIndex].name}"`,
        points: taskPoints
      };
      
      this.setData({
        earnTasks: newEarnTasks,
        'pointsData.totalPoints': newTotalPoints,
        'pointsData.todayPoints': newTodayPoints,
        pointsRecords: [newRecord, ...this.data.pointsRecords]
      });
      
      // 显示成功提示
      wx.showToast({
        title: `+${taskPoints}积分`,
        icon: 'success'
      });
    }, 1000);
  },

  // 加载更多积分记录
  loadMoreRecords() {
    if (!this.data.hasMoreRecords) return;
    
    // 模拟加载更多数据
    const newRecords = [
      { id: 6, date: '2023-05-28', type: '购物消费', desc: '购买家居用品', points: 150 },
      { id: 7, date: '2023-05-25', type: '兑换优惠券', desc: '兑换10元无门槛优惠券', points: -100 },
      { id: 8, date: '2023-05-20', type: '购物消费', desc: '购买电子产品', points: 300 }
    ];
    
    this.setData({
      pointsRecords: [...this.data.pointsRecords, ...newRecords],
      hasMoreRecords: false 
    });
  }
});
